<?php
class Login
{
    protected $_at;

    public function loginUser()
    {
        $at = Controller::getInstance()->at();

        if(isset($_SESSION['logged'])) {
            $verbindung = mysql_connect("localhost","root","");
            //or die ("Die Verbindung konnte nicht erstellt werden");
            mysql_select_db("game");
            $q = 'SELECT userId, password FROM Users WHERE name="' . $_SESSION['name'] . '" AND password="' . $_SESSION['password'] . '"';
            $dataUser = mysql_query($q);
            $row = mysql_fetch_assoc($dataUser);
            mysql_close($verbindung);
            if($row !== false) {
            } else {
                die("password lost");
            }
        }

        if('login' == $_REQUEST['action']) {
        //     $myFile = $documentRoot . "test.txt";
        //     $fh = fopen($myFile, 'a') or die("can't open file");
            $name = $_POST['logName'];
            $pass = md5($_POST['logPass']);

            $verbindung = mysql_connect("localhost","root","");
            $verbindung = mysql_connect("localhost","root","");
            //or die ("Die Verbindung konnte nicht erstellt werden");
            mysql_select_db("game");
            $q = 'SELECT userId, password FROM Users WHERE name="' . $name . '" AND password="' . $pass . '"';

            $myFile = "test.txt";
            $fh = fopen($myFile, 'a') or die("can't open file");
            fwrite($fh, "\n" . $q);

            $dataUser = mysql_query($q);
            $row = mysql_fetch_assoc($dataUser);
            echo print_r($row);
            mysql_close($verbindung);

            if(0 < count($row) && isset($row['password']) && is_numeric($row['userId'])) {
                session_start();
                $_SESSION['logged'] = $row['password'];
                $_SESSION['user'] = $row['user'];//$row['userId'];
                $at->assign('loggedIn', 'true');
            }
            fclose($fh);
        }
    }

    /**
     * @return smarty
     */
    public function getAt()
    {
        if(!$this->_at) {
            $this->_at = Controller::getInstance()->at();
        }
        return $this->_at;
    }

    public function setAt($at) {
        $this->_at = $at;
    }
}

